package ModelLayer;
import java.util.*;
import java.util.Iterator;

/**
 * Write a description of class DVDContainer here.
 * 
 * @author (Group2) 
 * @version (20)
 */
public class DVDCollection
{
    private static final DVDCollection instance = new DVDCollection();          //singleton

    private ArrayList<DVD> collection;      //instance variable;

    public DVDCollection()      //constructor of the class DVDCollection;
    {
        collection=new ArrayList();
    }

    public static DVDCollection getInstance()       //singleton of the class;
    {
        return instance;
    }

    public void addDVD(DVD dvd)     //adds a dvd to the collection;
    {
        collection.add(dvd);    
    }

    public void createDVD(String id, String title, String artist, String publicationDate)       //creates an object of class dvd and adds it to the collection;
    {
        DVD dvd=new DVD(id, title, artist, publicationDate);
        collection.add(dvd);
    }

    public boolean removeDVD(String id)     //removes a dvd from the collection;
    {
        Iterator<DVD> it = collection.iterator();
        boolean found=false;
        while(it.hasNext())
        {
            if(it.next().id.equals(id))
            {
                it.remove();
                found=true;
            }
            
        }
        return found;
     }
    
        
    public void readDVD(String id)      //returns the information about a dvd;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.print();
            }     
        }
    }

    public void updateDVDTitle(String id, String title)     //updates a specified DVDs title;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setTitle(title);
            }
        }
    }

    public void updateDVDArtist(String id, String artist)    //updates a specified DVDs artist
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setArtist(artist);
            }
        }
    }

    public void updateDVDPublicationDate(String id, String publicationDate)     //updates a specified DVDs publication date;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(id))
            {
                select.add(a);
                a.setPublicationDate(publicationDate);
            }
        }
    }

    public void addCopiesToDVD(String DVDId, int serialNumber, String purchaseDate, int purchasePrice, String state)        //adds copies to a dvds list;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(DVDId))
            {
                select.add(a);
                a.createCopy(serialNumber, purchaseDate, purchasePrice, state);
            }

        }
    }

    public void getCopiesOfDVDById(String DVDId)        //returns the copies of the dvd --search by id;
    {    
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.id.equals(DVDId))
            {
                select.add(a);
                a.getCopies();
            }

        }
    }

    public void getCopiesOfDVDByTitle(String DVDTitle)      //returns the copies of the dvd -- search by title;
    {    
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.title.equals(DVDTitle))
            {
                select.add(a);
                a.getCopies();
            }

        }
    }

    public void getCopyOfDVD(int serialNumber)      //returns a copy of a dvd  -- search by the copys serial number;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {

            ArrayList<Copy> selectcopy=a.getList();
            ArrayList<Copy> selectcopies=new ArrayList<Copy>();
            for(Copy b: selectcopy)
            {
                if(b.serialNumber==serialNumber)
                {
                    selectcopies.add(b);
                    b.print();
                }
            }
        }
    }

    public boolean checkCopiesOfDVD(String DVDTitle)        //checks if the dvd have copies available -- required for the loan;
    {    
        boolean found=false;
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            if(a.title.equals(DVDTitle))
            {
                select.add(a);
                if(select!=null)
                {
                    found=true;
                    a.getCopies();
                }
            }

        }
        return found;
    }

    public void modifyCopyStatus(int serialNumber, String state)        //modifies a copys status -- required to be able to recognise if there is a free copy to loan or not;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {

            ArrayList<Copy> selectcopy=a.getList();
            ArrayList<Copy> selectcopies=new ArrayList<Copy>();
            for(Copy b: selectcopy)
            {
                if(b.serialNumber==serialNumber)
                {
                    selectcopies.add(b);
                    b.setState(state);
                }
            }
        }
                
    }
    
    public String getCopyStatus(int serialNumber)       //returns the status of a copy -- required for the loaning;
    {
        String status=null;
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            
            ArrayList<Copy> selectcopy=a.getList();
            ArrayList<Copy> selectcopies=new ArrayList<Copy>();
            for(Copy b: selectcopy)
            {
                if(b.serialNumber==serialNumber)
                {
                    selectcopies.add(b);
                    status=b.getState();
                }
            }
        }
        return status;    
    }

    public ArrayList<DVD> getList()     //returns the list of dvds;
    {
        return collection;    
    }

    public void printAllDvds()      //prints all dvds information;
    {
        ArrayList<DVD> select=new ArrayList<DVD>();

        for(DVD a: collection)
        {
            a.getID();
            a.getTitle();
            a.getArtist();
            a.getPublicationDate();
            System.out.println("********************************");   
        }
    }

}
